<?php



$displayForm = true;
$currentForm = 'create-currency';

echo '
<p class="pageoptions">
    <a class="pageoptions" href="javascript: ISubscribe_Admin.showCurrencies(\'create-currency\');" 
    title="'.$this->Lang('create_currency').'">
        <img class="systemicon" 
        src="themes/NCleanGrey/images/icons/system/newobject.gif" 
        alt="'.$this->Lang('create_currency').'" />
    </a>
    <a class="pageoptions" href="javascript: ISubscribe_Admin.showCurrencies(\'create-currency\');" 
    title="'.$this->Lang('create_currency').'">
        '.$this->Lang('create_currency').'
    </a>

    <a class="pageoptions" href="javascript: ISubscribe_Admin.showCurrencies(\'edit-currency\');" >
        <img class="systemicon" src="themes/NCleanGrey/images/icons/system/newobject.gif" alt="'.$this->Lang('edit_currency').'" />
    </a>
    <a class="pageoptions"  href="javascript: ISubscribe_Admin.showCurrencies(\'edit-currency\');" title="'.$this->Lang('edit_currency').'">
        '.$this->Lang('edit_currency').'
    </a>

    <a class="pageoptions"  href="javascript: ISubscribe_Admin.showCurrencies(\'delete-currency\');"  title="'.$this->Lang('delete_currency').'">
        <img class="systemicon" src="themes/NCleanGrey/images/icons/system/newobject.gif" alt="'.$this->Lang('delete_currency').'" />
    </a>
    <a class="pageoptions" href="javascript: ISubscribe_Admin.showCurrencies(\'delete-currency\');"  title="'.$this->Lang('delete_currency').'">
        '.$this->Lang('delete_currency').'
    </a>
</p>


';




if(get('delete')==1){


    $curKey = preg_replace('/[^a-zA-Z0-9]/', '', get('currency_id'));

    if(Antz_IntelliForm::submitted('currencies')){
    
	   $DB->delete(cms_db_prefix().'isubscribe_subscription_prices', "currency_key = '{$curKey}'");
	   $DB->delete(cms_db_prefix().'isubscribe_currencies', "keyname = '{$curKey}'");
	   Antz::addSuccess($this->Lang('currency_deleted'));
	   Antz::redirect($actionUrls['currencies']);
	}
	
	
	$form = Antz::newObject('Antz_Form', 'currencies');
	
	$form->addSubmit($this->Lang('confirm_delete'));

	echo '<h2>'.$this->Lang('deleting', $DB->oneValue(cms_db_prefix().'isubscribe_currencies', 'title', "keyname = '{$curKey}'"));
	
	echo $form->render();
	
	$displayForm = false;
	
}



if(get('edit')==1){
	// wanting to edit a currency
	$curKey = preg_replace('/[^a-zA-Z0-9]/', '', get('currency_id'));
	if(!($curData = $DB->fetchRow("SELECT * FROM {$this->config['db_prefix']}isubscribe_currencies WHERE keyname = '{$curKey}'"))){
		Antz::addError($this->Lang('invalid_currency'));
		Antz::redir($actionUrls['currencies']);
	};

	// now we have a valid currency to edit

	if(Antz_IntelliForm::submitted('currencies')){
		$error = false;

		$title = post('title');
		$key = preg_replace('/[^a-zA-Z0-9]/', '', post('key'));

		if($key==''){
			$error = true;
			Antz::addFormError('key');
		};

		if($key != $curKey && $DB->countRows(cms_db_prefix().'isubscribe_currencies', "keyname = '{$key}'")>0){
			$error = true;
			Antz::addFormError('key', $this->Lang('already_registered'));
		};

		if(trim($title)==''){
			$error = true;
			Antz::addFormError('title');
		};

		if(!$error){
			$dbParams = array(
				'keyname' => $key,
				'title' => $title
			);

			$DB->update(cms_db_prefix().'isubscribe_currencies', $dbParams, "keyname = '{$curKey}'");

			if($curKey != $key){
				$DB->update(cms_db_prefix().'isubscribe_subscription_prices', array('currency_key'=>$key), "currency_key = '{$curKey}'");
			}

			Antz::addSuccess($this->Lang('currency_updated'));
			Antz::redir($actionUrls['currencies']);

		}

	}

	$form = Antz::newObject('Antz_Form', 'currencies');
	$form->setAction(THIS_PAGE_URL);

	$block = $form->add('full');
	$fs = $block->add($this->Lang('edit_currency'));
	$fs->addInput('text', 'key', $this->Lang('keyname'), post('key', $curData['keyname']));
	$fs->addInput('text', 'title', $this->Lang('title'), post('title', $curData['title']));
	$fs->addInput('submit', 'submitBtn', '&nbsp;', $this->Lang('update'));

    $displayForm = false;
    $currentForm = 'edit-currency';

    echo '<div id="edit-currency">';
    echo '<h2 class="accessibility">'.$this->Lang('edit_currency').'</h2>';
	echo $form->render();
	echo '</div>';

}


if(post('create')==1){
	if(Antz_IntelliForm::submitted('currencies')){
		$error = false;

		$title = post('title');
		$key = preg_replace('/[^a-zA-Z0-9]/', '', post('key'));

		if($key==''){
			$error = true;
			Antz::addFormError('key');
		};


		if(trim($title)==''){
			$error = true;
			Antz::addFormError('title');
		};

		if(!$error){
			$dbParams = array(
				'keyname' => $key,
				'title' => $title
			);

			$DB->insert(cms_db_prefix().'isubscribe_currencies', $dbParams);

			Antz::addSuccess($this->Lang('currency_created'));
			Antz::redir($actionUrls['currencies']);

		}
	}

}








$currencies = $DB->fetchAll("SELECT * FROM {$this->config['db_prefix']}isubscribe_currencies ORDER BY keyname ASC");

// add a new currecy form
$form = Antz::newObject('Antz_Form', 'currencies');
$form->setAction($actionUrls['currencies']);

$block = $form->add('full');
$fs = $block->add($this->Lang('create_currency'));

$fs->addInput('hidden', 'create', 'create', '1');

$fs->addInput('text', 'key', $this->Lang('keyname'), post('key'));
$fs->addInput('text', 'title', $this->Lang('title'), post('title'));
$fs->addInput('submit', 'submitBtn', '&nbsp;', $this->Lang('create'));

echo '<div id="create-currency">';
echo '<h2 class="accessibility">'.$this->Lang('create_currency').'</h2>';
echo $form->render();
echo '</div>';
if(!$displayForm) echo '<script type="text/javascript">document.getElementById("create-currency").style.display="none"</script>';


// edit a currency
if($currentForm != 'edit-currency'){

    $form = Antz::newObject('Antz_Form', 'currencies');
    $form->setAction($actionUrls['currencies']);
    $form->setMethod('get');
    
    $block = $form->add('full');
    $fs = $block->add($this->Lang('edit_currency'));
    
    foreach($_GET as $k=>$v){
    	 $fs->addInput('hidden', $k, $k, $v);
    }
    
    $fs->addInput('hidden', 'edit', 'Edit', '1');
    
    $curSel = $fs->addSelect('currency_id', $this->Lang('currency'), post('currency_id'));
    foreach($currencies as $k=>$v){
    	$curSel->add($v['title'], $v['keyname']);
    };
    
    $fs->addInput('submit', 'submitBtn', '&nbsp;', $this->Lang('edit'));
    
    echo '<div id="edit-currency">';
    echo '<h2 class="accessibility">'.$this->Lang('edit_currency').'</h2>';
    echo $form->render();
    echo '</div>';
    echo '<script type="text/javascript">document.getElementById("edit-currency").style.display="none"</script>';

}else{    
    echo '<script type="text/javascript">ISubscribe_Admin.curCurrency = "edit-currency"</script>';
}


// dlete a currency
$form = Antz::newObject('Antz_Form', 'currencies');
$form->setAction($actionUrls['currencies']);
$form->setMethod('get');

$block = $form->add('full');
$fs = $block->add($this->Lang('delete_currency'));
    
foreach($_GET as $k=>$v){
  	 $fs->addInput('hidden', $k, $k, $v);
}
    
$fs->addInput('hidden', 'delete', $this->Lang('delete'), '1');

$curSel = $fs->addSelect('currency_id', $this->Lang('currency'), post('currency_id'));
foreach($currencies as $k=>$v){
	$curSel->add($v['title'], $v['keyname']);
};

$fs->addInput('submit', 'submitBtn', '&nbsp;', $this->Lang('delete'));

echo '<div id="delete-currency">';
echo '<h2>'.$this->lang('delete_currency').'</h2>';
echo $form->render();
echo '</div>';
echo '<script type="text/javascript">document.getElementById("delete-currency").style.display="none"</script>';















